<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class BaiduYuyin666Job implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    private $mp3File ;
    private $text;

    public function __construct($mp3File,$text)
    {
        $this->mp3File = $mp3File;
        $this->text = $text;
    }


    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->text_to_video($this->mp3File,$this->text);
    }


    /**
     * 文字转语音
     * @param string $mp3File 音频文件地址
     * @param string $text 要转的文字
     * */
    public function text_to_video($mp3File,$text)
    {
        //合成语音
        if(!isset($GLOBALS['baiduYuyinApi']))
        {
            $file   = app_path('').'/Libs/BaiduYuyin/AipSpeech.php';
            require $file;
            $appId  = "10483156";
            $apiKey = "51fozfgAIAYMhV08ObWXAcqh";
            $secretKey = "f747998496f8cb2640bf0d58bfb2eb82";
            $baiduApi = new \AipSpeech($appId,$apiKey,$secretKey);
            $GLOBALS['baiduYuyinApi'] = $baiduApi;
        }
        $baiduApi   = $GLOBALS['baiduYuyinApi'];
        $options    = [
            'per'=>1,
            'spd'=>6,
            'pit'=>7
        ];
        $result     = $baiduApi->synthesis($text,'zh',1,$options);
        file_put_contents($mp3File, $result);
    }

    public function fail(){
        mylogger('shibai');
    }
}
